<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

    <div id="app1"></div>
    <div id="app2"></div>
    <hr />
    <div id="app3"></div>
    <script src="../react-lib/babel.min.js"></script>
    <script src="../react-lib/react.development.js"></script>
    <script src="../react-lib/react-dom.development.js"></script>
    <!-- 使用 PropTypes 进行类型检查 -->
    <script src="../react-lib/prop-types.js"></script>
    <script type="text/babel">
        class Life extends React.Component{
            constructor(props){
                super(props)
                this.state = {
                    opacity: 1
                }

                this.remove = this.remove.bind(this)
            }
            remove(){
                ReactDOM.unmountComponentAtNode(document.getElementById('app1'))
            }
            render(){ 
                return (
                    <div>
                        {/** 第一层{}花括号表示JS代码，第二层花括号代表一个对象*/}
                        <h1 style={{opacity: this.state.opacity}}>{this.props.msg}</h1>
                        <button onClick={this.remove}>移除</button>
                    </div>
                )
            }
            componentDidMount(){
                this.timer = setInterval(() => {
                    console.log('setInterval');
                    const opacity = this.state.opacity <= 0? 1: this.state.opacity-.1
                    this.setState({opacity})
                }, 200);
            }
            componentWillUnmount(){
                clearInterval(this.timer)
            }
        }
        Life.propTypes = {
            msg: PropTypes.string.isRequired
        }
        ReactDOM.render(<Life msg="MSG"/>,document.getElementById('app1'))
    </script>
</body>

</html>